<template>
  <div class="page-container">
    <div class="control-container">
      <div class="right-panel">
        <div class="search-panel">
          <el-form :inline="true" label-width="100">
            <el-form-item label="用户名（或真实姓名）">
              <el-input v-model="query.name" />
            </el-form-item>
            <el-form-item label="职务">
              <el-select
                v-model="query.title"
                clearable
                class="ipt-item w-220"
                placeholder="请选择类别"
              >
                <el-option
                  v-for="(item,index) in titles"
                  :key="index+'title'"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="证书情况">
              <el-select
                v-model="query.certificateStatus"
                clearable
                class="ipt-item w-220"
                placeholder="请选择类别"
              >
                <el-option
                  v-for="(item,index) in certificateStatusList"
                  :key="index+'certificate_status'"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
            <el-button type="primary" @click="handleSelect">查询</el-button>
            <el-button type="primary" @click="resetForm">重置</el-button>
            <el-button v-if="hasBtn('user:export')" :loading="saveLoading" type="primary" @click="handleExport">导出</el-button>
          </el-form>
        </div>
        <div class="table-panel">
          <el-table
            ref="table"
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 300px)"
            :data="tableData"
            @selection-change="handleSelectionChange"
          >
            <el-table-column align="center" type="selection" width="80" />
            <el-table-column prop="tprojectDepartment.name" label="项目部" />
            <el-table-column prop="realName" label="姓名" />
            <el-table-column prop="gender" label="性别">
              <template slot-scope="scope">{{ getDictLabel('e031', scope.row.gender) }}</template>
            </el-table-column>
            <el-table-column prop="region" label="地区" :formatter="getArea" />
            <el-table-column prop="birthDate" label="出生年月">
              <template slot-scope="scope">
                <div>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="title" label="职务" :formatter="getTitle" />
            <el-table-column prop="certificateStatus" label="证书情况" :formatter="getCertificateStatus" />
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="handleDetail('detail',scope.row)">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <Pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <AddEnterpriseFormDialog
      :dialog-visible="dialogVisible"
      :dialog-type="dialogType"
      :obj="obj"
      @flushPage="initData"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getList } from '@/api/system/user'
import { getListDict } from '@/api/system/area'
import { hasBtnPermission as hasBtn } from '@/utils/buttons'
import { downloadFile, getDictLabel, parseTime } from '@/utils'
import AddEnterpriseFormDialog from '@/views/system/user/components/AddUserFormDialog'
import { download } from '@/api/user'
import { getSectionList } from '@/api/info/allInfo'
import { getRoleList } from '@/api/system/roles'

export default {
  name: 'User',
  components: {
    Pagination, AddEnterpriseFormDialog
  },
  data() {
    return {
      saveLoading: false,
      tableLoading: true,
      query: {
        type: 1,
        title: null,
        certificateStatus: null,
        name: '',
        total: 0,
        page: 1,
        limit: 10
      },
      tableData: [],
      dialogVisible: false,
      dialogType: '',
      obj: {},
      resetState: true,
      titles: [],
      certificateStatusList: [],
      areaList: []
    }
  },
  created() {
    getSectionList(['e039_03', 'e039_07']).then(value => {
      value.data.list.forEach(item => {
        if (item.infoType === 'e039_07') {
          // 职务
          this.titles.push(item)
        } else {
          // 证书情况
          this.certificateStatusList.push(item)
        }
      })
    })
    // 地区
    getListDict().then(value => {
      this.areaList = value.data.list
    })
    this.initData()
  },
  methods: {
    getList, getDictLabel, parseTime, getListDict, hasBtn,
    initData() {
      this.query = {
        name: '',
        total: 0,
        page: 1,
        limit: 10
      }
      this.handleSelect()
    },
    // 查询
    handleSelect() {
      this.tableLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      this.getList(query).then(res => {
        this.tableData = res.data.list
        this.query.total = res.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleSelectionChange(val) {
      if (val.length > 0) {
        this.resetState = false
      } else {
        this.resetState = true
      }
    },
    // 修改
    handleDetail(dialogType, row) {
      this.dialogVisible = true
      this.obj = row
      this.dialogType = dialogType
    },
    resetForm() {
      this.query = {
        ...this.query,
        title: null,
        certificateStatus: null,
        name: ''
      }
    },
    getTitle(row) {
      for (const i in this.titles) {
        if (this.titles[i].id === row.title) {
          return this.titles[i].name
        }
      }
    },
    getCertificateStatus(row) {
      for (const i in this.certificateStatusList) {
        if (this.certificateStatusList[i].id === row.certificateStatus) {
          return this.certificateStatusList[i].name
        }
      }
    },
    getArea(row) {
      for (const i in this.areaList) {
        if (this.areaList[i].areaid === row.region) {
          return this.areaList[i].areaname
        }
      }
    },
    getRoleIds() {
      getRoleList().then(value => {
        this.roles = value.data.obj
      })
    },
    closeDialog() {
      this.dialogVisible = false
    },
    // 导出
    handleExport() {
      this.saveLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      download(query).then(result => {
        downloadFile(result, '人员档案', 'xlsx')
        this.saveLoading = false
      }).catch(() => {
        this.saveLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
